#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char const *argv[])
{
    char sbuffer[1024] = {0};
    char rbuffer[1024] = {0};
    ssize_t sbytes;
    struct sockaddr_in sv_addr;
    int sockfd, ret;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    printf("Socket created successfully; sockfd = %d\n", sockfd);

    bzero(&sv_addr, sizeof(sv_addr));
    sv_addr.sin_family = AF_INET;
    sv_addr.sin_port = htons(atoi(argv[2]));
    sv_addr.sin_addr.s_addr = inet_addr(argv[1]);

    // 客户端建立连接
    ret = connect(sockfd, (struct sockaddr *) &sv_addr, sizeof(struct sockaddr));
    if (ret == -1) {
        perror("connect error");
        exit(EXIT_FAILURE);
    }
    // 不断循环发送
    for(;;) {
        strcpy(sbuffer, "hello world");
        sbytes = send(sockfd, sbuffer, strlen(sbuffer), 0);
        if (sbytes == -1 ) {
            perror("send error");
            // close(sockfd);
            exit(EXIT_FAILURE);
        }
        usleep(100);
    }

    close(sockfd);
    return 0;

}
